﻿using System.Collections;
using System.Collections.Generic;
using TeamTasker.Client.Presentation.Controls.SearchInTreeControl;
using TeamTasker.Contracts;

namespace TeamTasker.Client.Presentation.ViewModels
{
    public class TeamsViewModel
    {
        public TeamsViewModel()
        {
            var teamMembers = new List<TeamMember>
                                  {
                                      new TeamMember {User = new User {FullName = "Bob Smith"}},
                                      new TeamMember {User = new User {FullName = "Jack Brown"}},
                                      new TeamMember {User = new User {FullName = "Sam Jones"}},
                                      new TeamMember {User = new User {FullName = "Nick Samuel"}},
                                      new TeamMember {User = new User {FullName = "Linda Mason"}}
                                  };
            TeamMembers = new NodeModelHierarchicalCollection<object>(teamMembers as IEnumerable, GetMembers, CreateNewNode);
        }

        public INodeModelHierarchicalCollection TeamMembers { get; set; }

        private static IEnumerable GetMembers(object obj)
        {
            if (obj is Team)
                return ((Team) obj).Members;
            return null;
        }

        private static NodeModel CreateNewNode(object obj, int level)
        {
            if (obj is Team)
            {
                var team = (Team) obj;
                return new NodeModel(team, team.Name, level);
            }
            var employee = (TeamMember) obj;
            return new NodeModel(employee, employee.User.FullName, level);
        }
    }
}